package com.zhang.first.day02;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;

/**
 * @title: flatmap
 * @author: zhang
 * @date: 2022/1/15 22:11
 */
public class Example4 {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        env
                .fromElements("aaa", "bbb", "ccc")
                .flatMap(new FlatMapFunction<String, String>() {
                    @Override
                    public void flatMap(String value, Collector<String> out) throws Exception {
                        if ("aaa".equals(value)) {
                            out.collect(value);
                        } else if ("bbb".equals(value)) {
                            out.collect(value);
                            out.collect(value);
                        }
                    }
                })
                .print();

        env
                .fromElements("aaa", "bbb", "ccc")
                .flatMap((String value, Collector<String> out) -> {
                    if ("aaa".equals(value)) {
                        out.collect(value);
                    } else if ("bbb".equals(value)) {
                        out.collect(value);
                        out.collect(value);
                    }
                })
                //编译成字节码的时候，将Collector<String> 类型擦除为Collector<Object> 此处需要做类型注解
                .returns(Types.STRING)
                .print();


        env.execute();
    }
}
